/*
 * Copyright 2011 Impetus Infotech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.impetus.teamplan.utils;

import java.net.URL;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.cassandra.service.EmbeddedCassandraService;

import com.impetus.kundera.api.Document;
import com.impetus.kundera.classreading.ClasspathReader;
import com.impetus.kundera.classreading.Reader;
import com.impetus.kundera.ejb.EntityManagerFactoryImpl;
import com.impetus.kundera.loader.Configuration;
import com.impetus.kundera.metadata.MetadataManager;
import com.impetus.teamplan.Constants;

/**
 * <Provide Type description>
 * @author amresh.singh
 */
public class KunderaUtils {
	private static EmbeddedCassandraService cassandra;
	private static Configuration conf;
	private static EntityManager em;

	public void startCassandraServer () throws Exception {
        URL configURL = KunderaUtils.class.getClassLoader().getResource("storage-conf.xml");
        try {
            String storageConfigPath = "/" + configURL.getFile().substring(1).substring(0, configURL.getFile().lastIndexOf("/"));
            System.setProperty("storage-config", storageConfigPath);
        } catch (Exception e) {
            System.out.println("Could not find storage-config.xml sfile");
        }
       
        cassandra = new EmbeddedCassandraService();
        
        //cassandra.init();     
        
        
        //Thread t = new Thread(cassandra);
        //t.setDaemon(true);
        //t.start(); 
        System.out.println("---------CASANDRA STARTED..............");
    }
	
	
	public void loadEntities() {			
		Reader reader = new ClasspathReader(null);	
		reader.addValidAnnotations(Document.class.getName());				 
		EntityManagerFactory emf =  (EntityManagerFactoryImpl)Persistence.createEntityManagerFactory(Constants.PERSISTENT_UNIT_NAME);
		MetadataManager mdm = new MetadataManager(emf);
		reader.addAnnotationDiscoveryListeners(mdm);
		reader.read();	
		mdm.build();
	}
	
	public static EntityManager getEntityManager() {		
		if(em == null || ! em.isOpen()) {
			conf = new Configuration();
			em = conf.getEntityManager(Constants.PERSISTENT_UNIT_NAME);
		}		 
		return em;	
	}

	public static void destroyEntityManager() {		
		if(conf != null) {
			conf.destroy();
		}		
	}	
	
	public static void closeEntityManager(EntityManager em) {		
		if(em != null) {
			em.close();
			em = null;
		}
	}	
	
}
